package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.SystemService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 系统设置
 * @author : zhongchen
 * @date : 2020/2/13 13:51
 */
@RestController
@RequestMapping("system")
public class SystemController {

    @Reference
    private SystemService systemService;

    /**
     * 分页查询权限信息
     * @param queryPageBean
     * @return
     */
    @RequestMapping("findPermissionPage")
    public PageResult findPermissionPage(@RequestBody QueryPageBean queryPageBean){
        return systemService.findPermissionPage(queryPageBean);
    }

    /**
     * 新建权限
     * @param permission
     * @return
     */
    @RequestMapping("addPermission")
    public Result addPermission(@RequestBody Permission permission){
        if (permission == null) {
            return new Result(false, MessageConstant.ADD_PERMISSION_FAIL);
        }
        try {
            systemService.addPermission(permission);
            return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.ADD_PERMISSION_FAIL);
    }

    /**
     * 根据id删除权限信息
     * @param id
     * @return
     */
    @RequestMapping("deletePermission")
    public Result deletePermission(Integer id){
        try {
            systemService.deletePermission(id);
            return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
    }

    /**
     * 根据id查询权限信息
     * @param id
     * @return
     */
    @RequestMapping("findPermissionById")
    public Result findPermissionById(Integer id){
        try {
            Permission permission = systemService.findPermissionById(id);
            if (permission == null) {
                return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
            }
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, permission);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
    }

    /**
     * 编辑权限
     * @param permission
     * @return
     */
    @RequestMapping("editPermission")
    public Result editPermission(@RequestBody Permission permission){
        try {
            systemService.editPermission(permission);
            return new Result(true, MessageConstant.UPDATE_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.UPDATE_PERMISSION_FAIL);
    }

    /**
     * 查询所有权限信息
     * @return
     */
    @RequestMapping("findPermissionAll")
    public Result findPermissionAll(){
        try {
            List<Permission> permissionList = systemService.findPermissionAll();
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, permissionList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
    }


     /************************************角色管理**************************************************************/

    /**
     * 分页查询角色信息
     * @param queryPageBean
     * @return
     */
     @RequestMapping("findRolePage")
     public PageResult findRolePage(@RequestBody QueryPageBean queryPageBean){
         return systemService.findRolePage(queryPageBean);
     }

    /**
     * 新建角色信息
     * @param role
     * @param permissionIds
     * @return
     */
     @RequestMapping("addRole")
     public Result addRole(@RequestBody Role role, Integer[] permissionIds){
         try {
             systemService.addRole(role, permissionIds);
             return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
         } catch (Exception e) {
             e.printStackTrace();
         }
         return new Result(false, MessageConstant.ADD_ROLE_FAIL);
     }

    /**
     * 根据id查询角色和权限关系
     * @param id
     * @return
     */
     @RequestMapping("findRelationOnRoleAndPermission")
     public Result findRelationOnRoleAndPermission(Integer id){
         try {
             List<Integer> relationOnRoleAndPermissionList = systemService.findRelationOnRoleAndPermission(id);
             return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, relationOnRoleAndPermissionList);
         } catch (Exception e) {
             e.printStackTrace();
         }
         return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
     }

    /**
     * 根据id查询角色信息
     * @param id
     * @return
     */
     @RequestMapping("findRoleById")
     public Result findRoleById(Integer id){
         try {
             Role role = systemService.findRoleById(id);
             return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, role);
         } catch (Exception e) {
             e.printStackTrace();
         }
         return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
     }

    /**
     * 编辑角色信息
     * @param role
     * @param permissionIds
     * @return
     */
     @RequestMapping("editRole")
     public Result editRole(@RequestBody Role role, Integer[] permissionIds){
         try {
             systemService.editRole(role, permissionIds);
             return new Result(true, MessageConstant.UPDATE_ROLE_SUCCESS);
         } catch (Exception e) {
             e.printStackTrace();
         }
         return new Result(false, MessageConstant.UPDATE_ROLE_FAIL);
     }

    /**
     * 删除角色信息
     * @param id
     * @return
     */
     @RequestMapping("deleteRole")
     public Result deleteRole(Integer id){
         try {
             systemService.deleteRole(id);
             return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
         } catch (Exception e) {
             e.printStackTrace();
         }
         return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
     }


    /************************************用户管理**************************************************************/


    /**
     * 分页查询用户信息
     * @return
     */
    @RequestMapping("findUserPage")
    public PageResult findUserPage(@RequestBody QueryPageBean queryPageBean){
        return systemService.findUserPage(queryPageBean);
    }

    /**
     * 查询所有角色信息
     * @return
     */
    @RequestMapping("findRoleAll")
    public Result findRoleAll(){
        try {
            List<Role> roleList = systemService.findRoleAll();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, roleList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
    }

    /**
     * 添加用户信息
     * @param user
     * @param roleIds
     * @return
     */
    @RequestMapping("addUser")
    public Result addUser(@RequestBody User user, Integer[] roleIds){
        try {
            systemService.addUser(user, roleIds);
            return new Result(true, MessageConstant.ADD_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.ADD_USER_FAIL);
    }

    /**
     * 根据id查询与用户关联的角色信息
     * @param id
     * @return
     */
    @RequestMapping("findRelationOnUserAndRole")
    public Result findRelationOnUserAndRole(Integer id){
        try {
            List<Integer> relationOnUserAndRoleList = systemService.findRelationOnUserAndRole(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, relationOnUserAndRoleList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
    }

    /**
     * 根据id查询用户信息
     * @param id
     * @return
     */
    @RequestMapping("findUserById")
    public Result findUserById(Integer id){
        try {
            User user = systemService.findUserById(id);
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS, user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_USER_FAIL);
    }

    /**
     * 编辑用户信息
     * @param user
     * @param roleIds
     * @return
     */
    @RequestMapping("editUser")
    public Result editUser(@RequestBody User user, Integer[] roleIds){
        try {
            systemService.editUser(user, roleIds);
            return new Result(true, MessageConstant.UPDATE_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.UPDATE_USER_FAIL);
    }

    @RequestMapping("deleteUser")
    public Result deleteUser(Integer id){
        try {
            systemService.deleteUser(id);
            return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.DELETE_USER_FAIL);
    }


}
